const group_name = 'test'
const tags = ['api', group_name]
const Joi = require('joi')
const JWT = require('jsonwebtoken')
const {
  jwtHeaderDefine
} = require('../../utils/router-helper');
module.exports = [{
  method: 'post',
  path: `/test/getToken`,
  config: {
    description: '测试生成token',
    auth: false,
    tags:tags,
    validate: {
      payload: {
        id: Joi.string().description('用户id')
      }
    }
  },
  handler: (request, h) => {
    let token = generateJWT({
      id: request.payload.id
    })
    return {
      token
    }
  }
},{
  method: 'get',
  path: `/test/detail`,
  config: {
    tags: tags,
    description: '测试验证token',
    validate: {
      ...jwtHeaderDefine
    }
  },
  handler: (request, h) => {
    console.log(request.auth.credentials);
    const response = h.response({
      message: 'You used a Valid JWT Token to access /restricted endpoint!'
    });
    response.header("authorization", request.headers.Authorization);
    return response;
  }
}]
const generateJWT = (jwtInfo) => {
  const payload = {
    id: jwtInfo.id,
    exp: Math.floor(new Date().getTime() / 1000) + 7 * 24 * 60 * 60,
  };
  return JWT.sign(payload, 'serverApi');
}
